// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) ansi lnc 

package com.example.airkissdemo.protocol;

import android.util.Log;
import com.example.airkissdemo.Util.Util;
import com.example.airkissdemo.network.UDPClientSingleton;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.concurrent.Semaphore;

public class AirKissProtocol
{
    public AirKissProtocol(String ssid, String passwd)
    {
        mOriData = buildOriginalData(ssid, passwd);
        mValidPayload = buildValidPayload(mOriData);
        mMagicCodeField = getMagicCodeField(ssid, mOriData.length);
        mPrefixCodeField = getPrefixCodeField(passwd.length());
        mSequencesData = getSequencesData(mValidPayload);
    }

    private byte[] buildOriginalData(String s, String s1)
    {
        byte byte0 = randomByte();
        byte abyte0[] = new byte[1 + (s.length() + s1.length())];
        try
        {
            System.arraycopy(s1.getBytes("UTF8"), 0, abyte0, 0, s1.length());
            abyte0[s1.length()] = byte0;
            System.arraycopy(s.getBytes("UTF8"), 0, abyte0, 1 + s1.length(), s.length());
        }
        catch(UnsupportedEncodingException unsupportedencodingexception)
        {
            Log.e("AirKissDemo.AirKissProtocol", "dataStr.getBytes error");
            unsupportedencodingexception.printStackTrace();
        }
        return abyte0;
    }

    private byte[] buildValidPayload(byte abyte0[])
    {
        int i = (int)Math.ceil((double)(abyte0.length << 3) / ((double)USE_BITS - 1.0D));
        byte abyte1[] = new byte[i];
        int j = 0;
        do
        {
            if(j >= i)
                return abyte1;
            int k = j * (-1 + USE_BITS) >> 3;
            int l = 7 & j * (-1 + USE_BITS);
            int i1 = (0xff & (0xff & abyte0[k]) << l) >> 1 + (8 - USE_BITS);
            int j1;
            if(j + 1 == i)
                j1 = 0;
            else
                j1 = (0xff & abyte0[k + 1]) >> 8 - l >> 1 + (8 - USE_BITS);
            abyte1[j] = (byte)(i1 | j1);
            j++;
        } while(true);
    }

    private int[] getMagicCodeField(String s, int i)
    {
        int ai[];
        byte abyte0[];
        byte byte0;
        ai = new int[4];
        abyte0 = new byte[s.length()];
        byte0 = 0;
        byte byte1;
        System.arraycopy(s.getBytes("UTF8"), 0, abyte0, 0, s.length());
        byte1 = Util.crc8_bytes(abyte0, abyte0.length);
        byte0 = byte1;
_L2:
        if(i < 16)
            i += 128;
        ai[0] = MAGIC_CODE_FLAG | 0xf & i >> 4;
        ai[1] = 0x10 | MAGIC_CODE_FLAG | i & 0xf;
        ai[2] = 0x20 | MAGIC_CODE_FLAG | 0xf & byte0 >> 4;
        ai[3] = 0x30 | MAGIC_CODE_FLAG | byte0 & 0xf;
        return ai;
        UnsupportedEncodingException unsupportedencodingexception;
        unsupportedencodingexception;
        unsupportedencodingexception.printStackTrace();
        if(true) goto _L2; else goto _L1
_L1:
    }

    private int[] getPrefixCodeField(int i)
    {
        int ai[] = new int[4];
        byte abyte0[] = new byte[1];
        abyte0[0] = (byte)i;
        byte byte0 = Util.crc8_bytes(abyte0, 1);
        ai[0] = 0x40 | MAGIC_CODE_FLAG | 0xf & i >> 4;
        ai[1] = 0x50 | MAGIC_CODE_FLAG | i & 0xf;
        ai[2] = 0x60 | MAGIC_CODE_FLAG | 0xf & byte0 >> 4;
        ai[3] = 0x70 | MAGIC_CODE_FLAG | byte0 & 0xf;
        return ai;
    }

    private int[] getSequenceField(byte byte0, byte abyte0[], int i, int j)
    {
        byte abyte1[] = new byte[j + 1];
        int ai[] = new int[j + 2];
        abyte1[0] = byte0;
        System.arraycopy(abyte0, i, abyte1, 1, j);
        byte byte1 = Util.crc8_bytes(abyte1, j + 1);
        ai[0] = SEQUENCE_HEADER_FLAG | byte1 & 255 >> 2 + (8 - USE_BITS);
        ai[1] = SEQUENCE_HEADER_FLAG | byte0 & 255 >> 2 + (8 - USE_BITS);
        int k = 0;
        do
        {
            if(k >= j)
                return ai;
            ai[k + 2] = DATA_FLAG | 0xff & abyte0[i + k];
            k++;
        } while(true);
    }

    private int[] getSequencesData(byte abyte0[])
    {
        int i = (int)Math.ceil((1.0D * (double)abyte0.length) / 4D);
        int ai[] = new int[(i << 1) + abyte0.length];
        int j = 0;
        do
        {
            if(j >= i)
                return ai;
            int k;
            int l;
            int ai1[];
            if(j == i - 1)
                k = abyte0.length - j * 4;
            else
                k = 4;
            l = j * 4;
            ai1 = getSequenceField((byte)j, abyte0, l, k);
            System.arraycopy(ai1, 0, ai, l + (j << 1), ai1.length);
            j++;
        } while(true);
    }

    private byte randomByte()
    {
        return (byte)(127 - (new Random()).nextInt(256));
    }

    public int[] getMagicCode()
    {
        return mMagicCodeField;
    }

    public byte[] getOriData()
    {
        return mOriData;
    }

    public int[] getPrefixCode()
    {
        return mPrefixCodeField;
    }

    public int[] getSequencesData()
    {
        return mSequencesData;
    }

    public int startDataBroadcast()
    {
        int i;
        int ai[];
        byte abyte0[];
        int j;
        i = 0;
        ai = mPrefixCodeField;
        abyte0 = new byte[1 << USE_BITS];
        j = 0;
_L5:
        if(j < ai.length) goto _L2; else goto _L1
_L1:
        int l = 0;
_L6:
        int k;
        if(l >= mSequencesData.length)
        {
            k = i;
        } else
        {
label0:
            {
                i = UDPClientSingleton.instance().sendData(abyte0, mSequencesData[l], "255.255.255.255", 7001, 5L);
                if(i == 0)
                    break label0;
                k = i;
            }
        }
_L4:
        return k;
_L2:
        i = UDPClientSingleton.instance().sendData(abyte0, ai[j], "255.255.255.255", 7001, 5L);
        if(i == 0)
            break; /* Loop/switch isn't completed */
        k = i;
        if(true) goto _L4; else goto _L3
_L3:
        j++;
          goto _L5
        l++;
          goto _L6
    }

    public int startPrecursorBroadcast()
    {
        int i;
        int ai[];
        final byte data[];
        int j;
        int l;
        i = 0;
        ai = mMagicCodeField;
        final byte precursor[] = new byte[4];
        precursor[0] = 1;
        precursor[1] = 2;
        precursor[2] = 3;
        precursor[3] = 4;
        data = new byte[1 << -1 + USE_BITS];
        final Semaphore sem = new Semaphore(0);
        Thread thread = new Thread(new Runnable() {

            public void run()
            {
                long l1;
                long l2;
                l1 = System.currentTimeMillis();
                l2 = l1;
_L2:
                if(l2 - l1 >= 2000L)
                {
                    sem.release();
                    return;
                }
                int i1 = 0;
                do
                {
label0:
                    {
                        if(i1 < 4)
                            break label0;
                        l2 = System.currentTimeMillis();
                    }
                    if(true)
                        continue;
                    UDPClientSingleton.instance().sendData(data, precursor[i1], "255.255.255.255", 7001, 10L);
                    i1++;
                } while(true);
                if(true) goto _L2; else goto _L1
_L1:
            }

            final AirKissProtocol this$0;
            private final byte val$data[];
            private final byte val$precursor[];
            private final Semaphore val$sem;

            
            {
                this$0 = AirKissProtocol.this;
                data = abyte0;
                precursor = abyte1;
                sem = semaphore;
                super();
            }
        }
);
        Log.d("AirKissDemo.AirKissProtocol", (new StringBuilder("start precursor time:")).append(System.currentTimeMillis()).toString());
        thread.start();
        try
        {
            sem.acquire();
        }
        catch(InterruptedException interruptedexception)
        {
            interruptedexception.printStackTrace();
        }
        Log.d("AirKissDemo.AirKissProtocol", (new StringBuilder("stop precursor time:")).append(System.currentTimeMillis()).toString());
        j = 0;
_L3:
        if(j < 20) goto _L2; else goto _L1
_L1:
        l = i;
_L4:
        return l;
_L2:
label0:
        {
            for(int k = 0; k < ai.length; k++)
                break label0;

            j++;
        }
          goto _L3
label1:
        {
            i = UDPClientSingleton.instance().sendData(data, ai[k], "255.255.255.255", 7001, 5L);
            if(i == 0)
                break label1;
            l = i;
        }
          goto _L4
    }

    private static final int DATAS_IN_ONE_SEQUENCE = 4;
    private static int DATA_FLAG = 0;
    private static final long INTERVAL_OF_PACKET_DATA = 5L;
    private static final long INTERVAL_OF_PACKET_GUIDE_CODE = 10L;
    private static int MAGIC_CODE_FLAG = 0;
    private static final int NUMBERS_OF_MAGICCODE = 20;
    private static final int NUMBERS_OF_PRECURSOR = 20;
    private static final int SEND_PRECURSOR_DURATION_MS = 2000;
    private static int SEQUENCE_HEADER_FLAG = 0;
    private static final String TAG = "AirKissDemo.AirKissProtocol";
    private static final String TARGET_ADDR = "255.255.255.255";
    private static final int TARGET_PORT = 7001;
    private static int USE_BITS;
    private int mMagicCodeField[];
    private byte mOriData[];
    private int mPrefixCodeField[];
    private int mSequencesData[];
    private byte mValidPayload[];

    static 
    {
        USE_BITS = 9;
        MAGIC_CODE_FLAG = 0;
        SEQUENCE_HEADER_FLAG = 1 << -2 + USE_BITS;
        DATA_FLAG = 1 << -1 + USE_BITS;
    }
}
